package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description CustomerReservationServiceImpl
 * @Author Lwj
 * @Date 2024-08-31-16:30
 */
@Service
@Slf4j
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;


    /**
     * 查询取消预约数量
     *
     * @return
     */
    @Override
    public Integer getCount(Long updateBy) {
        LocalDateTime startTime = LocalDateTime.now().withHour(0).withMinute(0).withMinute(0);
        LocalDateTime endTime = LocalDateTime.now().withHour(23).withMinute(59).withMinute(59);
        Integer count = customerReservationMapper.getCount(startTime, endTime, updateBy);
        return count;
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<TimeCountVo> countBytime(Long time) {
        LocalDateTime startTime = LocalDateTimeUtil.of(time);
        /*String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));
        System.out.printf(format);*/
        LocalDateTime endTime = startTime.plusHours(24);
        List<TimeCountVo> timeCountVos = customerReservationMapper.countBytime(startTime, endTime);
        return timeCountVos;
//        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));
    }

    /**
     * 新增预约
     *
     * @param reservationDto
     */
    @Override
    public void add(ReservationDto reservationDto) {
        Long userId = UserThreadLocal.getUserId();
        Integer count = getCount(userId);
        if (count >= 3) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        //不超过三次，可以进行预约
        Reservation reservation = new Reservation();
        //属性拷贝
        BeanUtil.copyProperties(reservationDto, reservation);
        reservation.setStatus(ReservationStatus.PENDING.ordinal());
        reservation.setCreateBy(userId);
        try {
            customerReservationMapper.insert(reservation);
        } catch (Exception e) {
            log.info(e + "");
            throw new BaseException(BasicEnum.TIME_ALREADY_RESERVATED_BY_PHONE);
        }
    }

    @Override
    public PageResponse<ReservationVo> getPage(String name, Integer pageNumb, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNumb, pageSize);
        Page<ReservationVo> page = customerReservationMapper.selectgetPage(name, status);
        PageResponse<ReservationVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void cancle(Long id) {
        Integer status = ReservationStatus.CANCELED.ordinal();
        customerReservationMapper.update(id, status);
    }
}
